<%@page import="nl.hva.web.mvcwebapp.models.Course"%>
<%@page import="nl.hva.web.mvcwebapp.models.CourseUser"%>
<%@page import="nl.hva.web.mvcwebapp.doa.CourseDAO"%>
<%@page import="java.util.List"%>
<%
    HttpSession session_user = request.getSession(true);
    String firstname = (String) session_user.getAttribute("user_firstname");
    int user_id = 0;
    int user_type = 0;
    if (firstname != null) {
        user_id = Integer.valueOf("" + session_user.getAttribute("user_id"));
        user_type = Integer.valueOf("" + session_user.getAttribute("user_type"));
    }


%>
<%@ page session="true" errorPage="/chaterror.jsp" import="java.util.Set,java.util.Iterator,java.util.Map, nl.hva.web.mvcwebapp.chat.*"%>
<HTML>
    <HEAD>
        <TITLE> Chat - Room List</TITLE>
        <LINK rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/chat/chat.css">
        <link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/style.css">
        <SCRIPT language="JavaScript">
            <!--
            if(window.top != window.self)
            {
                window.top.location = window.location;
            }
            //-->
        </SCRIPT>
    </HEAD>

    <BODY class="linen orange">
        <%


            String nickname = (String) session.getAttribute("nickname");
            if (nickname == null || nickname == "") {
                response.sendRedirect(request.getContextPath() + "/chat/login.jsp");
                //System.out.println("Redirecting");
            } else {
                String roomname = request.getParameter("rn");
                String descr = request.getParameter("sd");
                boolean see = false;
                if (descr != null && descr.equals("y")) {
                    see = true;
                }
        %>
        <div class="wrapper">
            <div class="content-wrapper">
                <%@ include file="/chat/header.jsp" %>
                <TABLE width="80%" align="center">	
                    <!--<tr>
                            <td class="normal">Welcome <span class="chattername"><%=nickname%></span></td>
                    </tr>	
                    -->
                    <TR>
                        <TD width="100%">Select the room you want to enter or click view description to view description
                            about the room.
                        </TD>
                    </TR>
                </TABLE>
                <BR>
                <%


                    try {
                        ChatRoomList roomlist = (ChatRoomList) application.getAttribute("chatroomlist");
                        ChatRoom[] chatrooms = roomlist.getRoomListArray();
                        if (roomname == null) {
                            roomname = roomlist.getRoomOfChatter(nickname).getName();
                        }
                        roomname = roomname.trim();

                %>
                <DIV align="center">
                    <CENTER>
                        <FORM name="chatrooms" action="<%=request.getContextPath()%>/chat/start.jsp" method="post">
                            <TABLE width="80%" border="1" cellspacing="1" cellpadding="1" align="center">
                                <TR>
                                    <TD colspan="2" class="pagetitle">Room List</TD>
                                </TR>
                                <!-- ADMIN CHECK -->
                                <%if (user_type == 3) {
                                        for (int y = 0; y < chatrooms.length; y++) {
                                %>

                                <TR>
                                    <TD width="50%">
                                        <INPUT type=radio name="rn" value="<%=chatrooms[y].getName()%>"
                                               <%if (chatrooms[y].getName().equals(roomname)) {
                                                       out.write("checked");
                                                   }%>><%=chatrooms[y].getName()%></A>
                                    </TD>
                                    <%
                                        if (see == true && chatrooms[y].getName().equals(roomname)) {
                                    %>	
                                    <TD width="50%"><%=chatrooms[y].getDescription()%></TD>
                                    <%
                                    } else {
                                    %>
                                    <TD width="50%"><A href="<%=request.getContextPath()%>/chat/listrooms.jsp?rn=<%=chatrooms[y].getName()%>&sd=y">View Description</A></TD>
                                    <%
                                        }
                                    %>
                                </TR>
                                <%
                                        }
                                    }
                        
                        /////////////////////ADMIN CHECK END///////////////////////////
                        
                        
                        
                                    List<CourseUser> courselist_user = CourseDAO.isEnrolledUser(user_id);
                                    for (int i = 0; i < chatrooms.length; i++) {
                                        if (chatrooms[i].getName().equalsIgnoreCase("StartUp")) {
                                            continue;
                                        }
                                %>
                                <%

                                    for (int x = 0; x < courselist_user.size(); x++) {
                                        Course test = courselist_user.get(x).getCourse();
                                        int course_id = test.getCourseid();
                                        if (chatrooms[i].getId() == course_id) {
                                %>

                                <TR>
                                    <TD width="50%">
                                        <INPUT type=radio name="rn" value="<%=chatrooms[i].getName()%>"
                                               <%if (chatrooms[i].getName().equals(roomname)) {
                                                       out.write("checked");
                                                   }%>><%=chatrooms[i].getName()%></A>
                                    </TD>
                                    <%
                                        if (see == true && chatrooms[i].getName().equals(roomname)) {
                                    %>	
                                    <TD width="50%"><%=chatrooms[i].getDescription()%></TD>
                                    <%
                                    } else {
                                    %>
                                    <TD width="50%"><A href="<%=request.getContextPath()%>/chat/listrooms.jsp?rn=<%=chatrooms[i].getName()%>&sd=y">View Description</A></TD>
                                    <%
                                        }
                                    %>
                                </TR>
                                <%

                                        }
                                    }


                                %>

                                <%
                                        }
                                    } catch (Exception e) {
                                        System.out.println("Unable to get handle to Servlet Context: " + e.getMessage());
                                        e.printStackTrace();
                                    }
                                %>
                                <TR>
                                    <TD><INPUT class="button" type="Submit" value="Start Chat"></TD>
                                </TR>
                            </TABLE>
                        </FORM>
                    </CENTER>
                </DIV>
                <%
                    }
                %>
                <%@ include file="/chat/footer.jsp"%>
            </DIV>
        </DIV>
    </BODY>
</HTML>